package model

import (
	"errors"
	"fmt"
	"github.com/jinzhu/gorm"
)

//type LC struct {
//	Id  uint64 `gorm:"primary_key;auto_increment"`
//	Pid uint64 `gorm:"column:pid;not null" json:"pid" binding:"required"`
//	//看官uid
//	Uid uint64 `gorm:"column:uid;not null" json:"uid" binding:"required"`
//	//点赞或收藏
//	Type string `gorm:"column:type;not null" json:"type" binding:"required"`
//}

// 点赞关联表
type Like struct {
	Uid uint64 `gorm:"uniqueIndex:user_blog" json:"uid"` // 联合唯一索引
	Pid uint64 `gorm:"uniqueIndex:user_blog" json:"pid"`
}

// 收藏关联表
type Collect struct {
	Uid                 uint64 `gorm:"column:uid;uniqueIndex:user_blog" json:"uid"`
	Pid                 uint64 `gorm:"column:pid;uniqueIndex:user_blog" json:"pid"`
	User_info_model_uid uint64 `gorm:"column:user_info_model_uid;primary_key" json:"user_info_model_uid"`
	Post_pid            uint64 `gorm:"column:post_pid;primary_key" json:"post_pid"`
}

func GetLike(uid uint64, pid uint64) (*Post, error) {
	var existing Like
	r := DB.Self.Where("uid = ? AND pid = ?", uid, pid).First(&existing)

	fmt.Println(existing)
	tx := DB.Self.Begin()
	if tx.Error != nil {
		return nil, tx.Error
	}

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
			panic(r)
		}
	}()

	var err error
	if existing.Uid == 0 && existing.Pid == 0 {
		if errors.Is(r.Error, gorm.ErrRecordNotFound) {
			// 创建新点赞
			fmt.Println("创建新点赞了")
			like := Like{Uid: uid, Pid: pid}
			if err = tx.Create(&like).Error; err != nil {
				tx.Rollback()
				return nil, err
			}
			// 更新计数器
			err = updateLikeCount(tx, pid, 1)
		} else {
			tx.Rollback()
			return nil, r.Error
		}
	} else {
		// 取消点赞
		fmt.Println("取消点赞了")
		if err = tx.Delete(&existing).Error; err != nil {
			tx.Rollback()
			return nil, err
		}
		// 更新计数器
		err = updateLikeCount(tx, pid, -1)
	}

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit().Error; err != nil {
		return nil, err
	}

	// 查询最新数据
	var post Post
	if err = DB.Self.Where("pid = ?", pid).First(&post).Error; err != nil {
		return nil, err
	}
	return &post, nil
}

func updateLikeCount(tx *gorm.DB, pid uint64, delta int) error {
	return tx.Model(&Post{}).Where("pid = ?", pid).
		UpdateColumn("likecount", gorm.Expr("likecount + ?", delta)).Error
}

func GetCollect(uid uint64, pid uint64) (*Post, error) {
	var existing Collect
	r := DB.Self.Where("uid = ? AND pid = ?", uid, pid).First(&existing)

	fmt.Println(existing)
	tx := DB.Self.Begin()
	if tx.Error != nil {
		return nil, tx.Error
	}

	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
			panic(r)
		}
	}()

	var err error
	if existing.Uid == 0 && existing.Pid == 0 {
		if errors.Is(r.Error, gorm.ErrRecordNotFound) {
			// 创建新点赞
			fmt.Println("创建新收藏了")
			collect := Collect{Uid: uid, Pid: pid, User_info_model_uid: uid, Post_pid: pid}
			if err = tx.Create(&collect).Error; err != nil {
				tx.Rollback()
				return nil, err
			}
			// 更新计数器
			err = updateCollectCount(tx, pid, 1)
		} else {
			tx.Rollback()
			return nil, r.Error
		}
	} else {
		// 取消点赞
		fmt.Println("取消点赞了")
		if err = tx.Delete(&existing).Error; err != nil {
			tx.Rollback()
			return nil, err
		}
		// 更新计数器
		err = updateCollectCount(tx, pid, -1)
	}

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit().Error; err != nil {
		return nil, err
	}

	// 查询最新数据
	var post Post
	if err = DB.Self.Where("pid = ?", pid).First(&post).Error; err != nil {
		return nil, err
	}
	return &post, nil
}

func updateCollectCount(tx *gorm.DB, pid uint64, delta int) error {
	return tx.Model(&Post{}).Where("pid = ?", pid).
		UpdateColumn("collectcount", gorm.Expr("collectcount + ?", delta)).Error
}
